#include "proc.h"
#include "memory.h"

static void *pf = NULL;

void* new_page(void) {
  assert(pf < (void *)_heap.end);
  void *p = pf;
  pf += PGSIZE;
  return p;
}

void free_page(void *p) {
  panic("not implement yet");
}

/* The brk() system call handler. */
// int mm_brk(uint32_t new_brk) {
//   if(current->cur_brk == 0)
//   {
//     current->cur_brk = current->max_brk = new_brk;
//   }
//   else
//   {
//     if(new_brk>current->max_brk){
//       uint32_t first=PGROUNDUP(current->max_brk);
//       uint32_t end=PGROUNDDOWN (new_brk);
//       if((new_brk &0xfff)==0){
//         end-=PGSIZE;
//       }
//       for(uint32_t va=first;va<=end;va+=PGSIZE){
//         void *pa=new_page();
//         _map(&(current->as),(void*)va,pa);
//       }
//       current->max_brk=new_brk;
//     }
//     current->cur_brk=new_brk;
//   }
//     return 0;
// }

int mm_brk(uint32_t new_brk) {
  if (current->cur_brk == 0) {
      current->cur_brk = current->max_brk = new_brk;
  } 
  else {
    if (new_brk > current->max_brk) {
      // 计算需要映射的内存区域的大小，并按页对齐
      size_t bytes_to_map = new_brk - PGROUNDUP(current->max_brk);
      size_t npages = (bytes_to_map + PGSIZE - 1) / PGSIZE; //计算页面数
      
      // 为每个页面调用 _map 函数进行映射
      for (size_t i = 0; i < npages; ++i) {
          void *va = (void *)(PGROUNDUP(current->max_brk) + i * PGSIZE);
          void *pa = new_page(); 
          _map(&current->as, va, pa);
      }
      current->max_brk = new_brk;
    }
  }
  current->cur_brk = new_brk;
  return 0;
}

void init_mm() {
  pf = (void *)PGROUNDUP((uintptr_t)_heap.start);
  Log("free physical pages starting from %p", pf);

  _pte_init(new_page, free_page);
}
